

import java.util.Arrays;

public class ModTwo {

    public int getTotalNum(int target, int range) {

        int[] t = getBinForm(target);
        int ans = 0;
        for (int i = 10; i <= range; i++) {
            int[] cur = getBinForm(i);
            int[] add1 = add(t, cur);
            int[] add2 = getBinForm(target + i);
            if (checkEqual(add1, add2)) {
                System.out.println(i);
                ans++;
            }
        }
        return ans;
    }

    // 二进制加法
    private int[] add(int[] t, int[] cur) {
        int i = 0;
        int carry = 0;
        int maxLen = Math.max(t.length, cur.length);
        int[] res = new int[maxLen + 1]; // 因为可能有进位
        while (i < maxLen || carry != 0) {
            int add1 = i < t.length ? t[i] : 0;
            int add2 = i < cur.length ? cur[i] : 0;
            int ans = add1 + add2 + carry;
            if (ans >= 2) {
                carry = 1;
            } else {
                carry = 0;
            }
            res[i++] = ans >= 2 ? 0 : ans;
        }

        if(res[res.length-1] == 0) {
            return Arrays.copyOf(res,res.length-1 );
        }
        return res;
    }

    private boolean checkEqual(int[] cur, int[] add) {
        if(cur.length != add.length) return false;
        int i = 0;
        int j = 0;
        while (i < cur.length) {
            if (cur[i] != add[j]) {
                return false;
            } else {
                i++;
                j++;
            }
        }
        return true;
    }

    private int[] getBinForm(int target) {
        int len = getLen(target);
        int[] res = new int[len];
        int idx = 0;
        while (target != 0) {
            int cur = target % 10;
            if ((cur & 1) == 0) {
                res[idx++] = 0;
            } else {
                res[idx++] = 1;
            }
            target /= 10;
        }
        return res;
    }

    private int getLen(int target) {
        int len = 0;
        while (target != 0) {
            len++;
            target /= 10;
        }
        return len;
    }

    public static void main(String[] args) {
        int target = 23;
        int range = 99;
        int ans = new ModTwo().getTotalNum(target, range);
        System.out.println("Ans: " + ans);
    }
}
